package com.wmz.crm.settings.controller;

import com.wmz.crm.commons.constants.Constants;
import com.wmz.crm.commons.entity.ReturnObject;
import com.wmz.crm.commons.utils.DateFormateUtil;
import com.wmz.crm.settings.entity.User;
import com.wmz.crm.settings.mapper.UserMapper;
import com.wmz.crm.settings.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/settings/qx/user/toLogin.do")
    public String toLogin(){
        //请求转发到登录页面
        return "settings/qx/user/login";
    }

    @RequestMapping("/settings/qx/user/login.do")
    @ResponseBody
    public Object login(String loginAct, String loginPwd, String isRemPwd,
                        HttpServletRequest request, HttpSession session,
                        HttpServletResponse response){
        //封装参数
        Map<String,Object> map = new HashMap<>();
        map.put("loginAct",loginAct);
        map.put("loginPwd",loginPwd);
        //创建返回值的对象
        ReturnObject returnObject = new ReturnObject();
        returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
        //调用service层方法查询用户
        User user = userService.queryUserByLoginActAndLoginPwd(map);
        if(user == null){
            //登录失败，用户不存在
            returnObject.setMessage("用户不存在");
        }else {

            if(DateFormateUtil.formateDateTime(new Date()).compareTo(user.getExpireTime()) > 0){
                //登录失败，用户已经过期
                returnObject.setMessage("用户已过期");
            }else if("0".equals(user.getLockState())){
                //登录失败，用户被锁定
                returnObject.setMessage("用户被锁定");
            }else if(!user.getAllowIps().contains(request.getRemoteAddr())){
                //登录失败，用户ip受限
                returnObject.setMessage("用户ip受限");
            }else {
                //登录成功
                returnObject.setCode(Constants.RETURN_OBJECT_CODE_SUCCESS);
                //向session域中保存用户信息
                session.setAttribute(Constants.SESSION_USER,user);
                //十天免登录,使用cookie实现
                if("true".equals(isRemPwd)){
                    Cookie c1 = new Cookie("loginAct",loginAct);
                    Cookie c2 = new Cookie("loginPwd",loginPwd);
                    c1.setMaxAge(10*24*60*60);
                    c2.setMaxAge(10*24*60*60);
                    response.addCookie(c1);
                    response.addCookie(c2);
                }else {
                    Cookie c1 = new Cookie("loginAct","1");
                    Cookie c2 = new Cookie("loginPwd","2");
                    c1.setMaxAge(0);
                    c2.setMaxAge(0);
                    response.addCookie(c1);
                    response.addCookie(c2);
                }
            }
        }
        return returnObject;
     }


    @RequestMapping("/settings/qx/user/logout.do")
    public String logout(HttpServletResponse response,HttpSession session){
        //清除cookie
        Cookie c1 = new Cookie("loginAct","1");
        Cookie c2 = new Cookie("loginPwd","2");
        c1.setMaxAge(0);
        c2.setMaxAge(0);
        response.addCookie(c1);
        response.addCookie(c2);
        //销毁session
        session.invalidate();
        return "redirect:/";
    }
}
